package org.or5e.plugin.mp;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.sound.sampled.AudioInputStream;

import javazoom.spi.mpeg.sampled.convert.DecodedMpegAudioInputStream;

import org.apache.commons.lang3.StringEscapeUtils;
import org.or5e.core.BaseObject;
import org.or5e.plugin.mp.core.player.audio.AudioPlayer;
import org.or5e.plugin.mp.core.player.audio.PlayerException;


public class MusicPlayerSPI extends BaseObject implements MusicPlayer {
	private AudioPlayer _player = null;
	private Player_Mode _mode = null;
	private long currentPlayingPosition = 0;
	@Override
	public void initilize(String settings) {
		_player = new AudioPlayer();
		_player.addPlaybackListener(new MusicPlaybackListener());
		List<String> mixers = _player.getMixers();
		if(mixers.size()>0) {
			_player.setMixerName(mixers.get(0));
		}
		_mode = Player_Mode.INITILIZED;
	}
	public List<String> getAllAvailableMixers() {
		return _player.getMixers();
	}
	public void setMixer(String mixer) {
		_player.setMixerName(mixer);
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
	}

	@Override
	public void play(String fileName) {
		if(_mode == Player_Mode.INITILIZED || _mode == Player_Mode.STOP) {
			try {
				_mode = Player_Mode.LOADING;
				_player.open(new File(fileName));
				_player.play();
				_mode = Player_Mode.PLAYING;
			} catch (PlayerException e) {
				e.printStackTrace();
				_mode = Player_Mode.INITILIZED;
			}
		}
	}

	@Override
	public void play(URL urlResource) {
		if(_mode == Player_Mode.INITILIZED) {
			try {
				_mode = Player_Mode.LOADING;
				_player.open(urlResource);
				_player.play();
				_mode = Player_Mode.PLAYING;
			} catch (PlayerException e) {
				e.printStackTrace();
				_mode = Player_Mode.INITILIZED;
			}
		}
	}

	@Override
	public void stop() {
		if(_mode == Player_Mode.PLAYING) {
			_player.stop();
			_mode = Player_Mode.STOP;
		}
		
	}

	@Override
	public void pause() {
		if(_mode == Player_Mode.PLAYING) {
			_player.pause();
			currentPlayingPosition = _player.getPosition();
			_mode = Player_Mode.PAUSE;
		}
		
	}

	@Override
	public void resume() {
		if(_mode == Player_Mode.PAUSE) {
			try {
				_player.play();
				_player.seek(currentPlayingPosition);
				_mode = Player_Mode.PLAYING;
			} catch (PlayerException e) {
				e.printStackTrace();
				_player.stop();
				_mode = Player_Mode.STOP;
			}
		}		
	}

	@Override
	public void seek(int step) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void next() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setVolume(Integer volumeLevel) {
		try {
			_player.setGain(volumeLevel/10.0f);
		} catch (PlayerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Integer getVolume() {
		float audioLevel = _player.getGain() * 10;
		return Math.round(audioLevel);
	}

	@Override
	public void nextPlaylist() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prevPlaylist() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<String> getAllPlaylist() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setAndPlayPlaylist(String playlist) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<String> getEqualizers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEqualizer(String equalizer) {
		info("Setting Equalizer: "+equalizer);
		switch (equalizer) {
		case "JAZZ": 
			AudioInputStream audioInputStream = _player.getAudioInputStream();
			if(audioInputStream instanceof DecodedMpegAudioInputStream) {
				Map properties = ((DecodedMpegAudioInputStream) audioInputStream).properties();
				float[] eqSettings = (float[])properties.get("mp3.equalizer");
				int index = 0;
				int eqSettingsLength = eqSettings.length;
				for(; index< eqSettingsLength; index ++) {
					eqSettings[index] = MusicEqualizer32.JAZZ[index];
				}
				try {
					_player.setGain(0.75f);
				} catch (PlayerException e) {
					e.printStackTrace();
				}
			}
			break;
		case "FLAT": 
			break;
		case "POP": 
			break;
		case "ROCK": 
			break;

		default:
			break;
		}
	}

	@Override public String getName() {
		return "MusicPlayerSPI";
	}

	public static void main(String[] args) throws IOException{
		MusicPlayer mPlayer = new MusicPlayerSPI();
		mPlayer.initilize("");
		List<String> allAvailableMixers = mPlayer.getAllAvailableMixers();
		mPlayer.setMixer(allAvailableMixers.get(1));
		System.out.println("Player Initilized");
		String audioFile = StringEscapeUtils.unescapeHtml4("E:/Shared/ITunes/Songs/Realisation/005 - PalaBishegam.mp3");
		System.out.println(audioFile);
		mPlayer.play(audioFile);
		
		System.out.println("\nNext: ");
		System.in.read();
		mPlayer.setEqualizer("JAZZ");

		System.out.println("\nNext: ");
		System.in.read();
		mPlayer.setEqualizer("FLAT");

		System.in.read();

		mPlayer.stop();
		System.out.println("Main Thread Completed...");
	}
}
